package wc;

import java.io.*;
import java.util.ArrayList;


public class Main {
    public static String inputFile;
    public static String outputFile;
    public static boolean needC;
    public static boolean needW;
    public static boolean needL;
    public static boolean needO;
    public static boolean needS;        //输入参数中是否有“-s”
    public static boolean needA;        //输入参数中是否有“-a”
    public static boolean needE;        //输入参数中是否有“-e”

    public static void main(String[] args) {
        inputFile = "";
        StopList.stopList="";
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
            if ("-c".equals(args[i])) {
                needC = true;
            } else if ("-w".equals(args[i])) {
                needW = true;
            } else if ("-l".equals(args[i])) {
                needL = true;
            } else if ("-s".equals(args[i])) {
                needS = true;
            } else if ("-a".equals(args[i])) {
                needA = true;
            } else if ("-e".equals(args[i])) {
                needE = true;
                StopList.stopList = args[i + 1];
            } else if ("-o".equals(args[i])) {
                needO = true;
                outputFile = args[i + 1];
            } else {
                if (!args[i - 1].equals("-e") && !args[i - 1].equals("-o")) {

                    inputFile = args[i];
                }
            }

        }
        String outputStr = "";
        ArrayList<String> fileNames = new ArrayList<String>();
        if (!needS) {
            fileNames.add(inputFile);
        } else {
            S.s(fileNames);
        }
        int len = fileNames.size();
        String fn;

        for (int i = 0; i < len; i++) {
            fn = fileNames.get(i);
            System.out.println(fn);
            String fileShortName = fn.substring(fn.lastIndexOf("\\") + 1, fn.length());
            Wc wc;
            if (needC || needW || needL) {
                //统计基本信息
                if (needE) {
                    wc=BasicInfoWithSL.getBasicInfoWithSL(fn,StopList.stopList);
                } else {
                    wc=BasicInfo.basicInfo(fn);
                }

                if (needC) {

                    outputStr += fileShortName;
                    outputStr += ", 字符数: ";
                    outputStr += wc.getChars();
                    outputStr += "\r\n";
                }
                if (needW) {

                    outputStr += fileShortName;
                    outputStr += ", 单词数: ";
                    outputStr += wc.getWords();
                    outputStr += "\r\n";
                }
                if (needL) {

                    outputStr += fileShortName;
                    outputStr += ", 行数: ";
                    outputStr += wc.getLines();
                    outputStr += "\r\n";
                }
            }
            if (needA) {
                 wc = ComInfo.comInfo(fn);//统计复杂信息
                //file1.c, 代码行/空行/注释行: 5/2/3
                outputStr += fileShortName;
                outputStr += ", 代码行/空行/注释行: ";
                outputStr += wc.codeLines;
                outputStr += "/";
                outputStr += wc.empLines;
                outputStr += "/";
                outputStr += wc.comLines;
                outputStr += "\r\n";
            }

        }

        System.out.println(outputStr);
        if (!needO) {
            outputFile = "result.txt";
        }
        try {

            File writename = new File(outputFile);
            writename.createNewFile();
            BufferedWriter out = new BufferedWriter(new FileWriter(writename));
            out.write(outputStr);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}
